package com.ai.springaidemo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class ChatDemoController {

    @Autowired
    private ChatClient chatClient;

    @Autowired
    private OpenAiChatModel openAiChatModel;

    /**
     * openAi 聊天
     *
     * @param message
     * @return
     */
    @GetMapping("/ai/openAiChat")
    public Map openAiChat(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        return Map.of("generation", openAiChatModel.call(message));
    }

    /**
     * ChatClient使用(流式调用)
     * @param message
     * @param voice
     * @return
     */
    @GetMapping("/ai/chatClient")
    public Map<String, String> completion(
            @RequestParam(value = "message", defaultValue = "Tell me a joke") String message,
            @RequestParam(value = "voice", defaultValue = "default") String voice) {

        String systemPrompt = "Please respond in a tone suitable for voice: " + voice;

        String result = chatClient.prompt()
                .system(systemPrompt)
                .user(message)
                .call()
                .content();

        return Map.of("completion", result);
    }

}
